# 文件处理工具
import os.path

import yaml


def object_path():
    """
    获取项目的根目录
    :return: None
    """
    return os.path.dirname(os.path.dirname(__file__))


def read_yml(path):
    """
    读取yml文件的方法
    :param path: yml文件的路径
    :return: yml文件的数据信息
    """
    with open(path, 'r', encoding='utf-8') as f:
        return yaml.safe_load(f)


# a = read_yml(r"C:\Users\Laptop\Desktop\api_test_31\config\BasicProjectInfo.yml")
# print(a['project']['crmeb'])
# # {'crmeb': {'test': 'http://47.95.213.187:8899', 'prod': 'http://47.95.213.187:8890', 'dev': 'http://47.95.213.187:8891'}}

def write_extract_data(key, value):
    """
    提取中间变量并写入extract_data.yml文件中
    :param key: 你自定义的变量名
    :param value: 变量实际的值
    :return: None
    """
    with open(object_path() + '/data/extract_data.yml', 'a', encoding='utf-8') as f:
        data = {key: value}
        yaml.dump(data, stream=f, allow_unicode=True)


def read_extract_data(key):
    """
    在extract_data.yml文件中读取变量
    :param key: 变量名
    :return: 变量的值
    """
    with open(object_path() + '/data/extract_data.yml', 'r', encoding='utf-8') as f:
        data = yaml.safe_load(f)
        return data[key]


def clear_extract_data():
    """
    清除extract_data.yml文件中的所有内容
    :return: None
    """
    with open(object_path() + '/data/extract_data.yml', 'r+', encoding='utf-8') as f:
        f.truncate()
